﻿Imports Global_Visas_Business_Layer
Imports System
Imports System.Data
Imports System.ComponentModel
Imports System.Collections.Generic
Partial Public Class frmAdminAddLetterpackToSLS
    Public ReadOnly Property ComboLetterTypes() As ComboBox
        Get
            Return Me.Combo_LetterTypes
        End Get
    End Property
    Private listSLS As Integer
    Public Property List_SLS() As Integer
        Get
            Return listSLS
        End Get
        Set(ByVal value As Integer)
            listSLS = value
        End Set
    End Property
    Private frmStart As Integer
    Public Property Frm_Start() As Integer
        Get
            Return frmStart
        End Get
        Set(ByVal value As Integer)
            frmStart = value
        End Set
    End Property
    Private frmEnd As Integer
    Public Property Frm_End() As Integer
        Get
            Return frmEnd
        End Get
        Set(ByVal value As Integer)
            frmEnd = value
        End Set
    End Property
    Private frmClient As Integer
    Public Property Frm_Client() As Integer
        Get
            Return frmClient
        End Get
        Set(ByVal value As Integer)
            frmClient = value
        End Set
    End Property
    Public Sub SQL_LetterPackList()

        Try
            Dim StrSQL As String
            StrSQL = "SELECT D_LetterPacks.LetterPack_ID, D_LetterPacks.LetterPack_Name as LetterPackName , D_LetterPacks.LetterPack_ID as VisID "
            StrSQL = StrSQL & " FROM D_LetterPacks "
            StrSQL = StrSQL & " WHERE (LetterPack_LetterTypeID = " & Me.Combo_LetterTypes.SelectedIndex & ")"
            StrSQL = StrSQL & " Order By D_LetterPacks.LetterPack_Name"
            Dim letterpPacks As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)
            Me.Combo_LetterPack.ItemsSource = letterpPacks
            Me.Combo_LetterPack.Columns(0).IsVisible = False
            'Me.Combo_LetterPack.Columns(1).Header = String.Empty
            'Me.Combo_LetterPack.Columns(2).Header = String.Empty
            'Call GblFunc_Populate_List(StrSQL, "SLS_AddLetterPack", Form_FMA_AddLetterPackToSLS.Combo_LetterPack)
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminAddLetterpackToSLS-SQL_LetterPackList")
        End Try

    End Sub

    Private Sub Combo_LetterTypes_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles Combo_LetterTypes.SelectionChanged
        Try
            Call SQL_LetterPackList()
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminAddLetterpackToSLS-Combo_LetterTypes_SelectionChanged")
        End Try
    End Sub

    Private Sub Btn_Cancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_Cancel.Click
        Me.Close()
    End Sub

    Private Sub Btn_Save_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_Save.Click
        Try
           
            Dim StrSQL As String, Rst As DataTable
            Dim IntSLSID As Integer, IntLPID As Integer
            Dim IntSFrom As Integer, IntSTo As Integer

            IntSLSID = Me.List_SLS
            Dim selectedLetterPack As DataRow = Me.Combo_LetterPack.SelectedItem
            If selectedLetterPack Is Nothing Then
                MsgBox("Select the letter pack first.", , "Error")
                Exit Sub
            End If
            IntLPID = selectedLetterPack(0)

            'Check if this letterpack is already linked to this SLS
            StrSQL = "Select SLSLInk_ID from I_SL_SLSLink where SLSLink_SLSID = " & IntSLSID & " AND SLSLInk_LetterpackID = " & IntLPID
            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count > 0 Then
                Rst.Dispose()
                Call MsgBox("That Letterpack is already in this Stock Letter Set.", , "Error")
                Exit Sub
            End If

            Rst.Dispose()

            'Validations
            If Me.Frm_Start = 3 And Me.Combo_Start.SelectedIndex = -1 Then
                Call MsgBox("Please select a custom start stage or change to another option")
                Me.Combo_Start.Focus()
                Exit Sub
            End If

            If Me.Frm_End = 3 And Me.Combo_End.SelectedIndex = -1 Then
                Call MsgBox("Please select a custom end stage or change to another option")
                Me.Combo_End.Focus()
                Exit Sub
            End If

            If Me.Frm_Start = 3 And Me.Frm_End = 3 And Me.Combo_Start.SelectedValue > Me.Combo_End.SelectedValue Then
                Call MsgBox("End Stage cannot be before Start Stage", , "Error")
                Exit Sub
            End If

            Select Case Me.Frm_Start

                Case 1
                    IntSFrom = 0

                Case 2
                    IntSFrom = 100

                Case 3
                    IntSFrom = Me.Combo_Start.SelectedValue

            End Select

            Select Case Me.Frm_End

                Case 1
                    IntSTo = 99

                Case 2
                    IntSTo = 999

                Case 3
                    IntSTo = Me.Combo_End.SelectedValue

                Case 4
                    IntSTo = 0

            End Select

            Dim saveResult As Integer = Gblfunc_Add_SLSLink(IntSLSID, IntLPID, 1, Me.Frm_Client, IntSFrom, IntSTo)
            If saveResult = 1 Then
                MsgBox("The letterpack was saved with success.", , "Error")
                Me.Close()
            ElseIf saveResult = 0 Then
                MsgBox("The letterpack couldnot be saved. Please try again or contact IT!", , "Error")
            End If

            'Clean up and exit
            'Form_FMA_SLS.List_Letterpacks.Requery()
            'Call Form_FMA_SLS.List_Letterpacks_AfterUpdate()
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminAddLetterpackToSLS-Btn_Save_Click")
        End Try
    End Sub

    Public Function Gblfunc_Add_SLSLink(ByVal SLSID As Integer, ByVal LinkID As Integer, ByVal LinkType As Integer, ByVal type As Integer, ByVal StageFrom As Integer, ByVal StageTo As Integer) As Integer
        Try
            'Gblfunc_Add_SLSLink = 0

            Dim StrSQL As String, Rst As DataTable, IntOrder As Integer, StrMess As String

            StrSQL = "Select Max(SLSLink_Order) as MaxOrder from I_SL_SLSLInk where SLSLink_SLSID = " & SLSID
            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count = 0 Then
                IntOrder = 1
            Else
                If Rst.Rows(0)("MaxOrder").ToString() Is "" Then
                    IntOrder = 1
                Else
                    IntOrder = CInt(Rst.Rows(0)("MaxOrder")) + 1
                End If
            End If

            Rst.Dispose()

            'Put together admin message.
            If LinkType = 2 Then
                StrSQL = "Select SLS_Name from I_SL_StockLetterSets where SLS_ID = " & LinkID
                Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)
                If Rst.Rows.Count = 0 Then
                    Rst.Dispose()
                    Exit Function
                End If

                StrMess = "SLS number " & LinkID & " (" & Rst.Rows(0)("SLS_Name") & ")"
                Rst.Dispose()
            Else
                StrSQL = "Select Letterpack_Name from D_Letterpacks where Letterpack_ID = " & LinkID
                Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)
                If Rst.Rows.Count = 0 Then
                    Rst.Dispose()
                    Exit Function
                End If

                StrMess = "Letterpack number " & LinkID & " (" & Rst.Rows(0)("Letterpack_Name") & ")"
                Rst.Dispose()
            End If

            StrSQL = "Select SLS_Name from I_SL_StockLetterSets where SLS_ID = " & SLSID
            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)
            If Rst.Rows.Count = 0 Then
                Rst.Dispose()
                Exit Function
            End If

            StrMess = StrMess & vbCrLf & " added to SLS number " & SLSID & " (" & Rst.Rows(0)("SLS_Name") & ")"
            Rst.Dispose()

            StrSQL = "Insert Into I_SL_SLSLink"
            StrSQL = StrSQL & " (SLSLink_SLSID, SLSLink_LetterpackID, SLSLink_CustomerType, SLSLink_StageStart, SLSLink_StageEnd, SLSLink_Order, SLSLink_ChildSLSID)"
            StrSQL = StrSQL & " VALUES("
            StrSQL = StrSQL & SLSID & ", "

            If LinkType = 2 Then
                StrSQL = StrSQL & "0 , "
            Else
                StrSQL = StrSQL & LinkID & ", "
            End If

            StrSQL = StrSQL & type & ", "
            StrSQL = StrSQL & StageFrom & ", "
            StrSQL = StrSQL & StageTo & ", "
            StrSQL = StrSQL & IntOrder & ", "

            If LinkType = 2 Then
                StrSQL = StrSQL & LinkID & ")"
            Else
                StrSQL = StrSQL & "0 )"
            End If

            If CommonDataFunctions.ExecuteSQL(StrSQL) = False Then Exit Function

            Call (New TasksEvents).GblFunc_Add_SysEvent("Admin (SL)", StrMess)

            Return 1

        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminAddLetterpackToSLS-Gblfunc_Add_SLSLink")
            Return 0
        End Try

    End Function

    Private Sub Option4_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option4.Checked
        Me.Frm_Start = 0
        Me.Combo_Start.IsEnabled = False
    End Sub

    Private Sub frmAdminAddLetterpackToSLS_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        Me.Frm_Start = 3
        Me.Frm_End = 3
        Me.SQL_LetterPackList()
        Me.LoadTheCaseStages()
        Me.Combo_Start.IsEnabled = False
        Me.Combo_End.IsEnabled = False
    End Sub

    Private Sub LoadTheCaseStages()
        Dim strSQL As String = "select CaseStage_ID,CaseStage_Number,CaseStage_Title from L_casestages"
        Dim caseStagesList As DataTable = CommonDataFunctions.GetSQLDatatable(strSQL)
        With Me.Combo_Start
            .ItemsSource = CType(caseStagesList, IListSource).GetList
            .SelectedValuePath = "CaseStage_Number"
            .DisplayMemberPath = "CaseStage_Title"
        End With
        With Me.Combo_End
            .ItemsSource = CType(caseStagesList, IListSource).GetList
            .SelectedValuePath = "CaseStage_Number"
            .DisplayMemberPath = "CaseStage_Title"
        End With
    End Sub

    Private Sub Option6_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option6.Checked
        Me.Frm_Start = 1
        Me.Combo_Start.IsEnabled = False
    End Sub

    Private Sub Option8_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option8.Checked
        Me.Frm_Start = 3
        Me.Combo_Start.IsEnabled = True
    End Sub

    Private Sub Option13_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option13.Checked
        Me.Frm_End = 0
        Me.Combo_End.IsEnabled = False
    End Sub

    Private Sub Option15_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option15.Checked
        Me.Frm_End = 1
        Me.Combo_End.IsEnabled = False
    End Sub

    Private Sub Option7_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option7.Checked
        Me.Frm_End = 2
        Me.Combo_End.IsEnabled = False
    End Sub

    Private Sub Option17_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option17.Checked
        Me.Frm_End = 3
        Me.Combo_End.IsEnabled = True
    End Sub

    Private Sub Option22_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option22.Checked
        Me.Frm_Client = 0
    End Sub

    Private Sub Option24_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option24.Checked
        Me.Frm_Client = 1
    End Sub

    Private Sub Option26_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Option26.Checked
        Me.Frm_Client = 2
    End Sub
End Class
